package servlet;

import hibernate.AssetInfo;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.WorkbookUtil;

import DAO.ZCDJ;


/**
 * Servlet implementation class deleteAssetServlet
 */
public class saveAssetServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public saveAssetServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    private static String getFilename()
    {
		String filename = "ShrekAppleManagement_";
		Date today=Calendar.getInstance().getTime();//返回当前中国地区的日期时间
        SimpleDateFormat df = new SimpleDateFormat("yyMMdd");
		String strtoday = df.format(today);
		filename += strtoday;
		
		return filename;
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		
		String assetNo = request.getParameter("assetNo");		
		Vector vt = new Vector();
        
		int start = 0 , next = 0 , end;
		end = assetNo.lastIndexOf(",");

		while(next != end)
		{
			next = assetNo.indexOf(",", start + 1);
			String str = assetNo.substring(start + 1, next);
			start = next;
			vt.addElement(str);
		}
		
		if(vt.size()==0)
		{
			out.print("{success:true, msg:'There is no record to export'}");
		}
		else{
		List list = null;
		ZCDJ zcdj = new ZCDJ();
		list = zcdj.saveAssetByQuery(vt);
		

//		AssetInfo temp = (AssetInfo)list.get(0);
//		String ttt = temp.getAssetName();

		//开始存入excel文件
		try{
			Workbook wb = new HSSFWorkbook();  // or new XSSFWorkbook();
//			CellStyle cellStyle = wb.createCellStyle();
//			cellStyle.setLocked(true);
			
			String safeName = WorkbookUtil.createSafeSheetName(getFilename()); // returns " O'Brien's sales   "
			Sheet sheet = wb.createSheet(safeName);

//			CreationHelper createHelper = wb.getCreationHelper();
			Row row;
			AssetInfo temp;
			
			row = sheet.createRow((short)0);
			row.createCell(0).setCellValue("ID");
			row.createCell(1).setCellValue("ProductName");
			row.createCell(2).setCellValue("Barcode");
			row.createCell(3).setCellValue("Status");
			row.createCell(4).setCellValue("BelongTo");
			row.createCell(5).setCellValue("SKU");
			row.createCell(6).setCellValue("Comment");
			row.createCell(7).setCellValue("DeviceType");
			
			
			
			for(int i = 1;i<=list.size();++i)
			{
				row = sheet.createRow((short)i);
				temp = (AssetInfo)list.get(i-1);
				
				row.createCell(0).setCellValue(temp.getAssetNo());
				row.createCell(1).setCellValue(temp.getAssetName());
				row.createCell(2).setCellValue(temp.getAssetBarcode());
				row.createCell(3).setCellValue(temp.getAssetState());
				row.createCell(4).setCellValue(temp.getBelongTo());
				row.createCell(5).setCellValue(temp.getSku());
				row.createCell(6).setCellValue(temp.getComment());
				row.createCell(7).setCellValue(temp.getDeviceType());
			}
			
			sheet.protectSheet("123456");
			
			try{
				FileOutputStream fileOut = new FileOutputStream("C:/Tomcat 6.0/webapps/Shrek/"+getFilename()+".xls",false);
				wb.write(fileOut);
				fileOut.close();
				out.print("{success:true, msg:'succeed'}");
			}
			
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
